package baseFx.common.utils;

import baseFx.common.Var;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class StringUtils {
    public static String join(CharSequence delimiter, Object... items) {
        List<String> ss = new ArrayList<>();
        for (Object item : items) {
            if (item != null) {
                if (item instanceof Iterable) {
                    Iterable<?> iterable = (Iterable<?>) item;
                    iterable.forEach(i -> ss.add(String.valueOf(i)));
                } else {
                    ss.add(String.valueOf(item));
                }
            }
        }
        return String.join(delimiter, ss);
    }

    public static boolean isNullOrEmpty(String str, boolean isTrim) {
        if (str == null) {
            return true;
        }
        if (isTrim) {
            str = str.trim();
        }
        return str.length() < 1;
    }

    public static boolean isNullOrEmpty(String str) {
        return isNullOrEmpty(str, true);
    }

    public static String trim(String input, String str) {
        return trimLeft(trimRight(input, str), str);
    }

    public static String trimLeft(String input, String prefix) {
        return trimLeft(input, prefix, false);
    }

    public static String trimLeft(String input, String prefix, boolean ignoreCase) {
        Pattern pat = Pattern.compile("^" + prefix, ignoreCase ? Pattern.CASE_INSENSITIVE : 0);
        while (pat.matcher(input).find()) {
            input = input.substring(prefix.length());
        }
        return input;
    }

    public static String trimRight(String input, String suffix) {
        return trimRight(input, suffix, false);
    }

    public static String trimRight(String input, String suffix, boolean ignoreCase) {
        Pattern pat = Pattern.compile(suffix + "$", ignoreCase ? Pattern.CASE_INSENSITIVE : 0);
        while (pat.matcher(input).find()) {
            input = input.substring(0, input.length() - suffix.length());
        }
        return input;
    }

    public static String repeat(String item, int count, String split) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list.add(item);
        }
        return String.join(split == null ? "" : split, list);
    }

    public static String repeat(String item, int count) {
        return repeat(item, count, null);
    }

    public static boolean hasValue(String str) {
        return !isNullOrEmpty(str);
    }

    public static boolean equals(String str1, String str2) {
        if (str1 == null && str2 == null) {
            return true;
        }
        if (str1 != null && str2 != null) {
            return str1.equals(str2);
        }
        return false;
    }

    public static String padLeft(String src, int count, String s) {
        if (src.length() >= count) {
            return src;
        }
        return repeat(s, count - src.length()) + src;
    }

    public static String padLeft(String src, int count) {
        return padLeft(src, count, " ");
    }

    public static String padRight(String src, int count, String s) {
        if (src.length() >= count) {
            return src;
        }
        return src + repeat(s, count - src.length());
    }

    public static String padRight(String src, int count) {
        return padRight(src, count, " ");
    }

    public static String format(String template, Object... args) {
        Var<String> var = new Var<>(template);
        for (Object item : args) {
            if (item instanceof Iterable) {
                Iterable iterable = (Iterable) item;
                iterable.forEach(i -> var.set(format(var.get(), i.toString())));
            } else {
                var.set(var.get().replaceFirst("\\{\\}", item != null ? item.toString() : "null"));
            }
        }
        return var.get();
    }
}
